/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ListaDoctores.java
 *
 * Created on 08-abr-2009, 11:12:38
 */

package laboratorio;

import clientes.Doctor;
import clientes.Cliente;
import java.util.List;
import java.util.ListIterator;
import javax.persistence.Query;

/**
 *
 * @author rocio
 */
public class ListaDoctores extends javax.swing.JFrame {
     private Doctor doctorSeleccionado;
     private Doctores VentanaDoctorOld;
     private Trabajos VentanaTrabajosOld;
     private String tipo;
     private Query q1,q2;
     private List doctores,clientes;
     private Doctor doctor;
     private ListIterator it;
     private int fila;
     private String iDcliente;
     private Cliente cliente;
    /** Creates new form ListaDoctores */
    public ListaDoctores() {
        initComponents();
    }


    public ListaDoctores(Doctores doctorOld) {
        VentanaDoctorOld = doctorOld;
        initComponents();
        jButton3.setVisible(false);
        q1 = lpdPUEntityManager.createQuery("SELECT d FROM Doctor d");
        doctores =q1.getResultList() ;
        rellenaTabla(doctores);
        tipo="doctores";
    }

    ListaDoctores(Trabajos aThis, String text) {
        iDcliente=text;
        if(iDcliente.equals("")){
            VentanaTrabajosOld=aThis;
            initComponents();
            q1 = lpdPUEntityManager.createQuery("SELECT d FROM Doctor d");
            doctores =q1.getResultList() ;
            rellenaTabla(doctores);
            tipo="trabajos";
        }else{
            VentanaTrabajosOld=aThis;
            initComponents();
            Integer a=Integer.parseInt(text);
            q1 = lpdPUEntityManager.createQuery("SELECT d FROM Doctor d WHERE d.iDcliente="+a);
            doctores =q1.getResultList() ;
            rellenaTabla(doctores);
            tipo="trabajos";
        }
    }


    public void rellenaTabla(List doctores){
        fila=0;
        it=doctores.listIterator();
        while(it.hasNext()){
            doctor=(Doctor) it.next();
            jTable1.setValueAt(doctor.getIDdoctor(), fila, 0);
            jTable1.setValueAt(doctor.getNombre(), fila, 1);
            fila++;
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lpdPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("lpdPU").createEntityManager();
        doctorQuery = java.beans.Beans.isDesignTime() ? null : lpdPUEntityManager.createQuery("SELECT d FROM Doctor d");
        doctorList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : doctorQuery.getResultList();
        doctorQuery1 = java.beans.Beans.isDesignTime() ? null : lpdPUEntityManager.createQuery("SELECT d FROM Doctor d");
        doctorList1 = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : doctorQuery1.getResultList();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(laboratorio.LaboratorioApp.class).getContext().getResourceMap(ListaDoctores.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jButton1.setFont(resourceMap.getFont("jButton1.font")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                Salir(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Nº Doctor", "Nombre"
            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                seleccionarDoctor(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("jTable1.columnModel.title0")); // NOI18N
        jTable1.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("jTable1.columnModel.title1")); // NOI18N

        jButton2.setFont(resourceMap.getFont("jButton2.font")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                BotonDoctor(evt);
            }
        });

        jButton3.setFont(resourceMap.getFont("jButton3.font")); // NOI18N
        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                nuevoDoctor(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 371, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addComponent(jButton2)
                        .addGap(18, 18, 18)
                        .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1)
                        .addGap(8, 8, 8)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void Salir(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_Salir
        doctorSeleccionado=null;
        this.setVisible(false);
}//GEN-LAST:event_Salir

    private void BotonDoctor(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_BotonDoctor
        if(tipo.equals("doctores")){
            VentanaDoctorOld.setDoctorSelec(doctorSeleccionado);
            this.setVisible(false);
        }else if(tipo.equals("trabajos")){
            VentanaTrabajosOld.setDoctor(doctorSeleccionado);
            if(iDcliente.equals("")){
                q2=lpdPUEntityManager.createQuery("SELECT c FROM Cliente c WHERE c.iDcliente="+doctorSeleccionado.getIDcliente());
                clientes=q2.getResultList();
                it=clientes.listIterator();
                if(it.hasNext()){
                    cliente=(Cliente) it.next();
                    VentanaTrabajosOld.setCliente(cliente);
                    System.out.print("asta aki hemos llegado bien");
                }
            }
            this.setVisible(false);
        }
}//GEN-LAST:event_BotonDoctor

    private void seleccionarDoctor(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_seleccionarDoctor
        int row=jTable1.getSelectedRow();
        doctorSeleccionado=doctorList.get(row);
    }//GEN-LAST:event_seleccionarDoctor

    private void nuevoDoctor(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_nuevoDoctor
        Doctores d=new Doctores();
        d.nuevo();
        d.setVisible(true);

    }//GEN-LAST:event_nuevoDoctor


    public Doctor getDoctor(){
        return doctorSeleccionado;
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ListaDoctores().setVisible(true);
            }
        });
    }
//public Doctores doctor =new Doctores();
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.util.List<clientes.Doctor> doctorList;
    private java.util.List<clientes.Doctor> doctorList1;
    private javax.persistence.Query doctorQuery;
    private javax.persistence.Query doctorQuery1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.persistence.EntityManager lpdPUEntityManager;
    // End of variables declaration//GEN-END:variables

}
